package cn.itsource.org.service.impl;

import cn.itsource.basic.service.impl.BaseServiceImpl;
import cn.itsource.org.domain.Department;
import cn.itsource.org.domain.Employee;
import cn.itsource.org.mapper.DepartmentMapper;
import cn.itsource.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {
    @Autowired
    private DepartmentMapper mapper;


    @Override
    public List<Employee> findtree() {
        return mapper.findtree();
    }

    public void seleparentid(List<Department> childen){
        if(childen.size()!=0){
            for (Department childern: childen) {
                List<Department> seleparentid = mapper.seleparentid(childern.getId());

                childern.setChildren(seleparentid.size()==0? null : seleparentid);
                this.seleparentid(seleparentid);
            }
        }else{
        }
    }

    @Override
    public List<Department> selechid() {
        List<Department> seletreemager = mapper.seletreemager();
        for (Department manger:seletreemager){
            List<Department> childern = manger.getChildren();
            if(childern.size()==0){
                manger.setChildren(null);
            }
            this.seleparentid(childern);
        }
        return seletreemager;
    }
}
